package com.bxx.ermall.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bxx.ermall.common.dto.SiteDto;
import com.bxx.ermall.common.dto.SiteListDto;
import com.bxx.ermall.common.lang.Result;
import com.bxx.ermall.entity.GoodsSite;
import com.bxx.ermall.service.GoodsSiteService;
import com.bxx.ermall.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author buxiaoxing
 * @since 2021-02-02
 */
@RestController
public class GoodsSiteController {

    @Autowired
    GoodsSiteService goodsSiteService;


    //用户自带永久商品位
    @PostMapping("/firstAdd")
    public Result firstAdd(@RequestParam Integer userId){
        GoodsSite site = new GoodsSite();
        site.setEndTime(LocalDateTime.now().plusYears(50));
        site.setOwner(userId);
        site.setState(0);
        site.setType(1);
        goodsSiteService.save(site);
        return Result.succ(null);
    }

    //购买期限商品位
    @PostMapping("/otherAdd")
    public Result otherAdd(@Validated @RequestBody SiteDto siteDto){
        GoodsSite site = new GoodsSite();
        site.setType(siteDto.getType());
        site.setOwner(ShiroUtil.getProfile().getId());
        site.setGoodId(siteDto.getGoodId());
        site.setState(0);
        site.setEndTime(LocalDateTime.now().plusDays(siteDto.getDay()));
        return Result.succ(null);
    }

    //推荐位商品列表
    @GetMapping("/vipList")
    public Result vipList(){
        List<SiteListDto> data = goodsSiteService.vipList();
        return Result.succ(data);
    }

    //搜索商品列表
    @GetMapping("/normalList")
    public Result normalList(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam String keyWord){
        Page<SiteListDto> data = goodsSiteService.normalList(new Page<>(currentPage,pageSize), keyWord);
        return Result.succ(data);
    }

    //启动商品位
    @PostMapping("/siteOn")
    public Result siteOn(@RequestParam Integer id){
        GoodsSite temp = goodsSiteService.getById(id);
        temp.setState(1);
        goodsSiteService.updateById(temp);
        return Result.succ(null);
    }
    //关闭商品位
    @PostMapping("/siteOff")
    public Result siteOff(@RequestParam Integer id){
        GoodsSite temp = goodsSiteService.getById(id);
        temp.setState(0);
        goodsSiteService.updateById(temp);
        return Result.succ(null);
    }
    //上架商品
    @PostMapping("/onShelf")
    public Result onShelf(@RequestParam Integer id, @RequestParam Integer goodsId){
        GoodsSite temp = goodsSiteService.getById(id);
        temp.setGoodId(goodsId);
        goodsSiteService.updateById(temp);
        return Result.succ(null);
    }

    //下架商品
    @PostMapping("/onShelf")
    public Result offShelf(@RequestParam Integer id){
        GoodsSite temp = goodsSiteService.getById(id);
        temp.setGoodId(null);
        goodsSiteService.updateById(temp);
        return Result.succ(null);
    }
}

